package dd.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import dd.control.BackToMainScreenController;
import dd.model.Event;
import dd.model.Model;

public class ResultScreen extends JFrame {

	private JPanel contentPane;
	// private DrawingCanvas canvas;
	// private ArrayList<Edge> ledge;
	private JTextField txtEventId;
	private JTextField txtChoice;
	private JTextField txtResult;
	private JLabel lblThanks;
	private JButton btnBack;
	private JTextField txtEventType;
	private JTextField txtEventBehavior;
	private JTextField txtQuestion;
	private JTextField txtChoiceNum;
	private JTextField txtRoundNum;
	private JTextField txtCompleted;

	/**
	 * Create the frame.
	 */
	public ResultScreen(Event event) {
		prepareResultScreen();
		// Event event = Model.getInstance().getEvent();

		// this event information
		this.txtEventId.setText(event.getEventID());
		this.txtEventType.setText(event.getMode());
		this.txtEventBehavior.setText(event.getType());
		this.txtQuestion.setText(event.getEventName());
		this.txtChoiceNum.setText(String.valueOf(event.getNumberLines()));
		this.txtRoundNum.setText(String.valueOf(event.getNumberEdges()));
		this.txtCompleted.setText(event.getIscompleted() == true ? "Completed" : "Uncompleted");

		// my choice and my result
		this.txtChoice.setText(event.getChoice(event.getPostion()));
		this.txtResult.setText(event.getMyResult());

		this.btnBack.addActionListener(new BackToMainScreenController(this));
	}

	private void prepareResultScreen() {
		setResizable(false);
		setTitle("View The Result");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		if (Model.getInstance().getaef() != null) {
			int x = Model.getInstance().getaef().getX();
			int y = Model.getInstance().getaef().getY();
			setBounds(x + 50, y + 50, 530, 280);
		} else {
			setBounds(100, 100, 530, 280);
		}
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblEventId = new JLabel("Event ID:");
		lblEventId.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblEventId.setBounds(28, 14, 70, 22);
		contentPane.add(lblEventId);

		txtEventId = new JTextField();
		txtEventId.setBackground(Color.WHITE);
		txtEventId.setHorizontalAlignment(SwingConstants.CENTER);
		txtEventId.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtEventId.setEditable(false);
		txtEventId.setBounds(124, 15, 147, 20);
		contentPane.add(txtEventId);
		txtEventId.setColumns(10);

		JLabel lblResult = new JLabel("Your Result:");
		lblResult.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblResult.setBounds(305, 87, 97, 22);
		contentPane.add(lblResult);

		JLabel lblChoice = new JLabel("Your Choice:");
		lblChoice.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblChoice.setBounds(305, 47, 84, 22);
		contentPane.add(lblChoice);

		txtChoice = new JTextField();
		txtChoice.setBackground(Color.WHITE);
		txtChoice.setHorizontalAlignment(SwingConstants.CENTER);
		txtChoice.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtChoice.setEditable(false);
		txtChoice.setBounds(387, 47, 106, 20);
		contentPane.add(txtChoice);
		txtChoice.setColumns(10);

		txtResult = new JTextField();
		txtResult.setBackground(Color.WHITE);
		txtResult.setHorizontalAlignment(SwingConstants.CENTER);
		txtResult.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtResult.setEditable(false);
		txtResult.setBounds(387, 88, 106, 20);
		contentPane.add(txtResult);
		txtResult.setColumns(10);

		lblThanks = new JLabel("Thanks for playing!");
		lblThanks.setHorizontalAlignment(SwingConstants.CENTER);
		lblThanks.setForeground(new Color(255, 99, 71));
		lblThanks.setBackground(new Color(255, 69, 0));
		lblThanks.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblThanks.setBounds(325, 129, 147, 36);
		contentPane.add(lblThanks);

		btnBack = new JButton("<< Back");
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnBack.setBounds(335, 186, 116, 40);
		contentPane.add(btnBack);

		JLabel lblEventType = new JLabel("Event Type:");
		lblEventType.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblEventType.setBounds(28, 47, 70, 22);
		contentPane.add(lblEventType);

		txtEventType = new JTextField();
		txtEventType.setHorizontalAlignment(SwingConstants.CENTER);
		txtEventType.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtEventType.setEditable(false);
		txtEventType.setColumns(10);
		txtEventType.setBackground(Color.WHITE);
		txtEventType.setBounds(124, 48, 147, 20);
		contentPane.add(txtEventType);

		JLabel lblEventBehavior = new JLabel("Event Behavior:");
		lblEventBehavior.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblEventBehavior.setBounds(28, 80, 93, 22);
		contentPane.add(lblEventBehavior);

		txtEventBehavior = new JTextField();
		txtEventBehavior.setHorizontalAlignment(SwingConstants.CENTER);
		txtEventBehavior.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtEventBehavior.setEditable(false);
		txtEventBehavior.setColumns(10);
		txtEventBehavior.setBackground(Color.WHITE);
		txtEventBehavior.setBounds(124, 81, 147, 20);
		contentPane.add(txtEventBehavior);

		JLabel lblQuestion = new JLabel("Question:");
		lblQuestion.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblQuestion.setBounds(28, 113, 93, 22);
		contentPane.add(lblQuestion);

		txtQuestion = new JTextField();
		txtQuestion.setHorizontalAlignment(SwingConstants.CENTER);
		txtQuestion.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtQuestion.setEditable(false);
		txtQuestion.setColumns(10);
		txtQuestion.setBackground(Color.WHITE);
		txtQuestion.setBounds(124, 114, 147, 20);
		contentPane.add(txtQuestion);

		JLabel lblChoicesNum = new JLabel("Choice Num:");
		lblChoicesNum.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblChoicesNum.setBounds(28, 146, 93, 22);
		contentPane.add(lblChoicesNum);

		txtChoiceNum = new JTextField();
		txtChoiceNum.setHorizontalAlignment(SwingConstants.CENTER);
		txtChoiceNum.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtChoiceNum.setEditable(false);
		txtChoiceNum.setColumns(10);
		txtChoiceNum.setBackground(Color.WHITE);
		txtChoiceNum.setBounds(124, 147, 147, 20);
		contentPane.add(txtChoiceNum);

		JLabel lblRoundNum = new JLabel("Round Num:");
		lblRoundNum.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblRoundNum.setBounds(28, 179, 93, 22);
		contentPane.add(lblRoundNum);

		txtRoundNum = new JTextField();
		txtRoundNum.setHorizontalAlignment(SwingConstants.CENTER);
		txtRoundNum.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtRoundNum.setEditable(false);
		txtRoundNum.setColumns(10);
		txtRoundNum.setBackground(Color.WHITE);
		txtRoundNum.setBounds(124, 181, 147, 20);
		contentPane.add(txtRoundNum);

		JLabel lblCompleted = new JLabel("Completed?");
		lblCompleted.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblCompleted.setBounds(28, 212, 93, 22);
		contentPane.add(lblCompleted);

		txtCompleted = new JTextField();
		txtCompleted.setHorizontalAlignment(SwingConstants.CENTER);
		txtCompleted.setFont(new Font("Tahoma", Font.PLAIN, 12));
		txtCompleted.setEditable(false);
		txtCompleted.setColumns(10);
		txtCompleted.setBackground(Color.WHITE);
		txtCompleted.setBounds(124, 212, 147, 20);
		contentPane.add(txtCompleted);
	}

	/**
	 * For test purpose
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					// Model model = Model.getInstance();
					// Event e = model.getEvent();
					ResultScreen frame = new ResultScreen(new Event());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
